<?php

namespace App\Models;

//继承可以使用auth登录的模型类
use http\Env\Request;
use Illuminate\Foundation\Auth\User as AuthUser;

//软删除类
use Illuminate\Database\Eloquent\SoftDeletes;

class User extends AuthUser
{
    //调用定义的tiait类  和继承一样
    use SoftDeletes;
    //定义软删除字段
    protected  $dates =['delete_at'];
    //添加字段 create 添加数据有效
    //拒绝不添加的字段
    protected $guarded =[];
    //隐藏密码
    protected $hidden =['password'];
    //模型关联
    public function  role(){
        return $this->belongsTo(Role::class,'role_id');
    }
    //将虚拟字段追加到数据对象列表里去
    protected $appends = ['rolename'];
    //访问器
    public function getRolenameAttribute()
    {
        if($this->role_id){
            $role = Role::find($this->role_id);
            if($role){
                return $role->name;
            }
        }
        return "";
    }
}

